package com.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.entity.ClassEvaluation;
import com.entity.User;
import com.entity.UserRole;
import com.mapper.UserMapper;
import com.service.UserService;

@Service("userService")
public class UserServiceImp implements UserService {
	@Autowired
	private UserMapper usermapper;

	@Override
	public void delete_do(Integer id) throws Exception {
		usermapper.deleteByPrimaryKey(id);
	}

	@Override
	public void add(User user) throws Exception {
		usermapper.insertSelective(user);
	}

	
	@Override
	public User getUserByLogin(String username, String password) throws Exception {
		User user = new User();
		user.setUsername(username);
		user.setPassword(password);
		return usermapper.getUserByLogin(user);
	}
	
	@Override
	public User getUserById(Long id) throws Exception {
		User user = new User();
		user.setId(id);
		return usermapper.getUserById(user);
	}
	
	@Override
	public Integer modUserBySubmit(User user) throws Exception {
		return usermapper.modUserBySubmit(user);
	}
	
	@Override
	public List<User> getUserListByUser(User user) throws Exception {
		return usermapper.getUserListByUser(user);
	}
	
	@Override
	public Integer getEvaTimes(User user) throws Exception {
		return usermapper.getEvaTimes(user);
	}
	
	@Override
	public void addEva(ClassEvaluation ce) throws Exception {
		usermapper.addEva(ce);
	}
	
	@Override
	public List<UserRole> getUserRoleList() throws Exception {
		return usermapper.getAllUserRoleList();
	}
	
	@Override
	public List<User> getAllUsers(User user) throws Exception {
		return usermapper.getAllUsers(user);
	}
	
	@Override
	public Map<String, Object> getImportUserDetails(List<List<String>> rows) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		boolean isSubmit = true;
		List<User> userList = new ArrayList<User>();
		for (int i = 0; i < rows.size(); i++) {
			List<String> excelRow = rows.get(i);
			if (i == 0) { // 第一行是表头
				continue;
			}
			User u = new User();
			if (excelRow.size() != 6) {
				u.setErrorTips("导入的文件列数不等于2列!");
				isSubmit = false;
			} else {
				u.setUsername(excelRow.get(0));
				u.setPassword(excelRow.get(1));
				u.setUserCode(excelRow.get(2));
				u.setNickName(excelRow.get(3));
				u.setSex(Integer.parseInt(excelRow.get(4)));
				u.setUserType(Integer.parseInt(excelRow.get(5)));
				userList.add(u);
			}
		}
		map.put("isSubmit", isSubmit);
		map.put("userList", userList);
		return map;
	}
}
